package com.zqs.service.impl;

import com.zqs.entity.User;
import com.zqs.mapper.UserMapper;
import com.zqs.service.UserService;
import com.zqs.service.ex.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void reg(User user) {

        User result = userMapper.findByUsername(user.getUsername());
        if (result != null){
            throw new UsernameDuplicatedException("用户名被占用异常!");
        }
        // 对密码md5加密
        String oldPassword = user.getPassword();
        String salt = UUID.randomUUID().toString().toUpperCase();
        // 补全盐值
        user.setSalt(salt);
        String md5Password = getMd5Password(oldPassword, salt);
        user.setPassword(md5Password);

        // 补全is_delete
        user.setIsDelete(0);

        // 补全日志
        Date date = new Date();
        user.setCreatedUser(user.getUsername());
        user.setModifiedUser(user.getUsername());
        user.setModifiedTime(date);
        user.setCreatedTime(date);

        Integer row = userMapper.reg(user);
        if (row != 1){
            throw new InsertException("插入时发生未知异常!");
        }
    }

    @Override
    public User login(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user == null){
            throw new UsernameErrorException("用户名不存在");
        }
        String salt = user.getSalt();
        String md5Password = getMd5Password(password, salt);
        if (!md5Password.equals(user.getPassword())){
            throw new PasswordErrorException("密码错误!");
        }
        if (user.getIsDelete() == 1){
            throw new UsernameErrorException("用户名不存在!");
        }
        return user;
    }

    @Override
    public void updateUserInfo(User user,Integer uid,String username) {
        Integer row = userMapper.updateUserInfoByUid(user.getAvatar(),user.getPhone(),user.getEmail(),
                                            user.getGender(),uid,username,new Date());
        if (row != 1){
            throw new UpdateException("修改时发生未知异常!");
        }
    }

    @Override
    public User userInfo(String username) {
        User user = userMapper.findByUsername(username);
        if (user == null){
            throw new UsernameErrorException("用户不存在!");
        }
        return user;
    }
//
//    @Override
//    public void updateAvatar(String avatar, Integer uid, String username) {
//        Integer row = userMapper.updateAvatar(avatar, username, new Date(), uid);
//        if (row != 1){
//            throw new UpdateException("更新时发生未知异常!");
//        }
//    }

    @Override
    public void updatePassword(String OldPassword,String NewPassword, String username, Integer uid) {
        User result = userMapper.findByUid(uid);
        String OldMd5Password = getMd5Password(OldPassword, result.getSalt());
        if (!OldMd5Password.equals(result.getPassword())){
            throw new PasswordErrorException("原密码有误");
        }
        String md5Password = getMd5Password(NewPassword, result.getSalt());
        Integer row = userMapper.updatePasswordByUid(md5Password, username, new Date(), uid);
        if (row != 1){
            throw new UpdateException("修改时发生异常!");
        }
    }

    @Override
    public User findByUid(Integer uid) {
        return null;
    }

    public String getMd5Password(String password,String salt){
        for (int i = 0; i < 2; i++) {
            password = DigestUtils.md5DigestAsHex((salt + password + salt).getBytes());
        }
        return password;
    }
}
